package com.knowif.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.knowif.dao.FastAskOrderMapper;
import com.knowif.dao.TUserMapper;
import com.knowif.pojo.FastAskOrder;
import com.knowif.pojo.FastAskOrderExample;
import com.knowif.pojo.TUser;
import com.knowif.pojo.UserOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author ：Xh
 * @date ：Created in 2019/5/4 0004 21:28
 */
@Service
public class FastAskOrder_service {

    @Autowired
    private FastAskOrderMapper fastAskOrderMapper;
    @Autowired
    private TUserMapper tUserMapper;

    /**
     * FastAskOrder条件查询
     *
     * @param selectFastAskOrderExample
     * @return
     */
    public List<FastAskOrder> selectFastAskOrder(FastAskOrderExample selectFastAskOrderExample) {
        List<FastAskOrder> fastAskOrders = fastAskOrderMapper.selectByExample(selectFastAskOrderExample);
        fillUserOrder(fastAskOrders);
        return fastAskOrders;
    }

    /**
     * 通过id查询
     *
     * @param fastAskOrderId
     * @return
     */
    public FastAskOrder selectFastAskOrderById(int fastAskOrderId) {
        FastAskOrder fastAskOrder = fastAskOrderMapper.selectByPrimaryKey(fastAskOrderId);
        TUser tUser = tUserMapper.selectByPrimaryKey(fastAskOrder.getUserId());
        TUser tutor = tUserMapper.selectByPrimaryKey(fastAskOrder.getTutorId());
        fastAskOrder.setUser(tUser);
        fastAskOrder.setTutor(tutor);
        return fastAskOrder;
    }

    /**
     * 插入selectFastAskOrder
     *
     * @param selectFastAskOrder
     * @return
     */
    public int saveFastAskOrder(FastAskOrder selectFastAskOrder) {
        return fastAskOrderMapper.insert(selectFastAskOrder);
    }

    /**
     * 更新不为空的selectFastAskOrder属性
     *
     * @param selectFastAskOrder
     * @return
     */
    public int updateFastAskOrderNotNull(FastAskOrder selectFastAskOrder) {
        return fastAskOrderMapper.updateByPrimaryKeySelective(selectFastAskOrder);
    }

    /**
     * 更新所有selectFastAskOrder属性
     *
     * @param selectFastAskOrder
     * @return
     */
    public int updateFastAskOrder(FastAskOrder selectFastAskOrder) {
        return fastAskOrderMapper.updateByPrimaryKey(selectFastAskOrder);
    }

    /**
     * 删除selectFastAskOrder
     *
     * @param selectFastAskOrderId
     */
    public int deleteFastAskOrder(int selectFastAskOrderId) {
        return fastAskOrderMapper.deleteByPrimaryKey(selectFastAskOrderId);
    }

    /**
     * 条件查询
     *
     * @param params
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public PageInfo<FastAskOrder> screen(Map<String, Object> params, int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<FastAskOrder> fastAskOrders = fastAskOrderMapper.screen(params);
        fillUserOrder(fastAskOrders);
        PageInfo<FastAskOrder> pageInfo = new PageInfo<>(fastAskOrders);
        return pageInfo;
    }

    private void fillUserOrder(List<FastAskOrder> fastAskOrders) {
        if (fastAskOrders != null) {
            fastAskOrders.forEach(fastAskOrder -> {
                TUser tUser = tUserMapper.selectByPrimaryKey(fastAskOrder.getUserId());
                TUser tutor = tUserMapper.selectByPrimaryKey(fastAskOrder.getTutorId());
                fastAskOrder.setUser(tUser);
                fastAskOrder.setTutor(tutor);
            });
        }
    }
}
